package com.witmed.lib_base

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import com.witmed.IView
import com.witmed.lib_common.utils.LoadDialog

abstract class BaseFragment<V:ViewDataBinding,VM:BaseViewModel<BaseModel>>:Fragment(),IView {

    lateinit var v:V
    lateinit var vm:VM
    lateinit var showDialog:Observer<String>
    lateinit var hiddenDialog:Observer<String>
    lateinit var startActivity:Observer<Map<String,Any>>
    lateinit var closeActivity:Observer<String>

    var observableList = mutableListOf<BaseLiveData<*>>()

    var observerList= mutableListOf<Observer<*>>()

    lateinit var dialog:LoadDialog

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        initDB(inflater,container)
        initVM()
        //创建ViewModel下livedata的观察者
        createLiveData()
        registerUIChange()
        initView()
        //给vm添加生命周期观察
        lifecycle.addObserver(vm)
        return v.root
    }


    private fun registerUIChange() {
        vm.getUiChangedListener()?.getStartActivityData()?.observeForever(startActivity)
        vm.getUiChangedListener()?.getCloseActivityData()?.observeForever(closeActivity)
        vm.getUiChangedListener()?.getShowDialogData()?.observeForever(showDialog)
        vm.getUiChangedListener()?.getHiddenDialogData()?.observeForever(hiddenDialog)
    }

    private fun createLiveData() {
        startActivity=object : Observer<Map<String, Any>> {
            override fun onChanged(value: Map<String, Any>) {
                startActivity()
            }
        }
        closeActivity=object : Observer<String> {
            override fun onChanged(value: String) {
                closeActivity()
            }
        }
        showDialog=object : Observer<String> {
            override fun onChanged(value: String) {
                showDialog()
            }
        }
        hiddenDialog=object : Observer<String> {
            override fun onChanged(value: String) {
                hiddenDialog()
            }
        }
    }

    fun showDialog(){
        dialog= LoadDialog(requireContext())
        dialog.show()
    }

    fun hiddenDialog(){
        if(dialog!=null){
            dialog.dismiss()
        }
    }

    fun closeActivity(){

    }


    abstract fun startActivity()

    override fun initVM() {
        vm=createVM()

    }

    abstract fun createVM(): VM


    fun initDB( inflater: LayoutInflater,container: ViewGroup?) {
        v=DataBindingUtil.inflate(inflater,initLayout(),container,false)
    }

    override fun onDestroy() {
        super.onDestroy()
        //防止内存泄露
        if(v!=null){
            v.unbind()
        }
        vm.getUiChangedListener()?.getStartActivityData()?.removeObserver(startActivity)
        vm.getUiChangedListener()?.getCloseActivityData()?.removeObserver(closeActivity)
        vm.getUiChangedListener()?.getShowDialogData()?.removeObserver(showDialog)
        vm.getUiChangedListener()?.getHiddenDialogData()?.removeObserver(hiddenDialog)
        lifecycle.removeObserver(vm)
    }

}